import numpy as np
from core.alogrithm import Algorithm


class RandomAlgorithm(Algorithm):
    def __init__(self, threshold=0.8):
        self.threshold = threshold

    def __call__(self, board, clock):
        workers = board.workers
        nodes = board.nodes_which_has_waiting_task
        candidate_task = None
        candidate_worker = None
        all_candidates = []

        for worker in workers:
            for node in nodes:
                # print(node.task.task_type,worker.skill_types)
                # todo: 需要node 排序，node結點個數相關信息
                print("random algrithm")
                if worker.accommodate(node):
                    all_candidates.append((worker, node))
                    if np.random.rand() > self.threshold:
                        candidate_worker = worker
                        candidate_node = node
                        break
        if len(all_candidates) == 0:
            return None, None
        if candidate_node is None:
            pair_index = np.random.randint(0, len(all_candidates))
            return all_candidates[pair_index]
        else:
            return candidate_worker, candidate_node
